﻿@using System.Web.Mvc.Html;
@using Vit.Common.Collections;
@model IEnumerable<dynamic>
@{
    int depth = ViewData["Depth"].ConvertTo<int>(99);
    int lastDepth = ViewData["LastDepth"].ConvertTo<int>(0);
    Func<dynamic, object> itemTemplate = ViewData["ItemTemplate"] as Func<dynamic, object>;
    ViewDataDictionary viewData = new ViewDataDictionary();
    viewData.Add("Depth", depth);
    viewData.Add("LastDepth", lastDepth + 1);
    viewData.Add("ItemTemplate", itemTemplate); 
}
@if (Model != null && Model.Count() > 0 && lastDepth < depth)
{
    int idx = 0;
    int total = Model.Count();
    <ul>
        @foreach (var o in this.Model)
        {
            <li class="@(o.Selected ? "selected" : "") @(idx == 0 ? "first" : "") @(idx == total - 1 ? "last" : "")">
                @if (itemTemplate != null)
                { 
                    @itemTemplate(o) 
                }
                else
                {
                    <span>@o.Caption</span>
                }
                @if (o.ChildNodes != null)
                {
                    @Html.Partial("_TreeView", (IEnumerable<dynamic>)o.ChildNodes, viewData)
                }
            </li>
                idx++;
        }
    </ul>
}